<?php
//Checkout -> 02 Contacts
//checkout02contacts.php

  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['email-op'] == "Submit") {
      //Form field check NG
      if (empty($_POST["email1"])) {
        printAlert("error", "Oh snap!", "E-mail field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["email2"])) {
        printAlert("error", "Oh snap!", "Reinsert E-mail field is empty.", $_SERVER['PHP_SELF']);
      } else {//Form field check OK
        //E-mail equal NG
        if ($_POST["email1"] != $_POST["email2"]) {
          printAlert("error", "Oh snap!", "E-mail adrress is not identical.", $_SERVER['PHP_SELF']);
        } else {//E-mail equal OK
          //E-mail validation OK
          if (filter_var($_POST["email1"], FILTER_VALIDATE_EMAIL)) {
            $_SESSION['checkOut']['email'] = TRUE;
            $_SESSION['checkOut']['email-address'] = $_POST["email1"];
          } else {//E-mail validation NG
            printAlert("error", "Oh snap!", "E-mail adrress is not valid.", $_SERVER['PHP_SELF']);
          }
        }
      }
    }
    if ($_POST['email-op'] == "Change") {
      unset($_SESSION['checkOut']['email']);
      unset($_SESSION['checkOut']['email-address']);
    }
    if ($_POST['address-op'] == "Submit") {
      //Form field check NG
      if (empty($_POST["fullname"])) {
        printAlert("error", "Oh snap!", "Fullname field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["street1"])) {
        printAlert("error", "Oh snap!", "Address field is empty.", $_SERVER['PHP_SELF']);
      /*
      } elseif (empty($_POST["street2"])) {//optional
        printAlert("error", "Oh snap!", "Address2 field is empty.", $_SERVER['PHP_SELF']);
      */
      } elseif (empty($_POST["city"])) {
        printAlert("error", "Oh snap!", "City field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["state"])) {
        printAlert("error", "Oh snap!", "State field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["country"])) {
        printAlert("error", "Oh snap!", "Country field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["postcode"])) {
        printAlert("error", "Oh snap!", "Postcode field is empty.", $_SERVER['PHP_SELF']);
      } elseif (empty($_POST["phone"])) {
        printAlert("error", "Oh snap!", "Phone field is empty.", $_SERVER['PHP_SELF']);
      /*
      } elseif (empty($_POST["email"])) {
        printAlert("error", "Oh snap!", "E-mail field is empty.", $_SERVER['PHP_SELF']);
      */
      } else {//Form field check OK
        $_SESSION['checkOut']['address']          = TRUE;
        $_SESSION['checkOut']['address-fullname'] = $_POST["fullname"];
        $_SESSION['checkOut']['address-street1']  = $_POST["street1"];
        $_SESSION['checkOut']['address-street2']  = $_POST["street2"];
        $_SESSION['checkOut']['address-city']     = $_POST["city"];
        $_SESSION['checkOut']['address-state']    = $_POST["state"];
        $_SESSION['checkOut']['address-country']  = $_POST["country"];
        $_SESSION['checkOut']['address-postcode'] = $_POST["postcode"];
        $_SESSION['checkOut']['address-phone']    = $_POST["phone"];
        //$_SESSION['checkOut']['address-email']  = $_POST["email"];
      }
    }
    if ($_POST['address-op'] == "Change") {
      unset($_SESSION['checkOut']['address']);
      unset($_SESSION['checkOut']['address-fullname']);
      unset($_SESSION['checkOut']['address-street1']);
      unset($_SESSION['checkOut']['address-street2']);
      unset($_SESSION['checkOut']['address-city']);
      unset($_SESSION['checkOut']['address-state']);
      unset($_SESSION['checkOut']['address-country']);
      unset($_SESSION['checkOut']['address-postcode']);
      unset($_SESSION['checkOut']['address-phone']);
      //unset($_SESSION['checkOut']['address-email']);
    }
  }
  
  if (isset($_SESSION['checkOut']['address'])) {
    $form2_input_disable = " disabled='disabled'";
    $form2_btn_txt = "Change";
    $form2_btn_clr = "btn-inverse";
    $form_continue_disable = "";
    $form_continue_url = " href='".$_SERVER['PHP_SELF']."?do=bag"."'";
  } else {
    $form2_input_disable = "";
    $form2_btn_txt = "Submit";
    $form2_btn_clr = "btn-info";
    $form_continue_disable = " disabled";
    $form_continue_url = "";
  }  
  
  if ($_SESSION['checkOut']['logintype'] == "GUEST") {
  
    if (isset($_SESSION['checkOut']['email'])) {
      $form1_input_disable = " disabled='disabled'";
      $form1_input_reinsert = FALSE;
      $form1_btn_txt = "Change";
      $form1_btn_clr = "btn-inverse";
    } else {
      $form1_input_disable = "";
      $form1_input_reinsert = TRUE;
      $form1_btn_txt = "Submit";
      $form1_btn_clr = "btn-info";
    }
  
    if ((isset($_SESSION['checkOut']['email'])) && (isset($_SESSION['checkOut']['address']))) {
      $form_continue_disable = "";
      $form_continue_url = " href='".$_SERVER['PHP_SELF']."?do=bag"."'";
    } else {
      $form_continue_disable = " disabled";
      $form_continue_url = "";
    }
?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="application/x-www-form-urlencoded" class="form-horizontal">
            <fieldset>
              <div class="control-group">
                <label class="control-label" for="input01">E-mail</label>
                <div class="controls">
                  <input type="text" class="span3" id="input01" name="email1" value="<?php echo $_SESSION['checkOut']['email-address'];?>"<?php echo $form1_input_disable;?> />
                </div>
              </div>
<?php
    if ($form1_input_reinsert) {
?>
              <div class="control-group">
                <label class="control-label" for="input02">Reinsert E-mail</label>
                <div class="controls">
                  <input type="text" class="span3" id="input02" name="email2"<?php echo $form1_input_disable;?> />
                </div>
              </div>
<?php
    }
?>
              <div class="form-actions">
                <input type="submit" name="email-op" value="<?php echo $form1_btn_txt;?>" class="btn <?php echo $form1_btn_clr;?>" />
                <a<?php echo $form_continue_url; ?> class="btn btn-success<?php echo $form_continue_disable; ?>">Continue &raquo;</a>
              </div>
            </fieldset>
          </form>
<?php
  }
  
  $cols = array(0, 1);
  $conds = NULL;
  $query = $db->db_select("postage", $cols, $conds);
  $result = $db->database->query($query);
  $tmpPostg = $result->fetchAll(SQLITE_ASSOC);
  $form_postage_country_select = "";
?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="application/x-www-form-urlencoded" class="form-horizontal">
            <fieldset>
              <div class="control-group">
                <label class="control-label" for="input01">Fullname</label>
                <div class="controls">
                  <input type="text" class="span3" id="input01" name="fullname" value="<?php echo $_SESSION['checkOut']['address-fullname'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input02">Address</label>
                <div class="controls">
                  <input type="text" class="span3" id="input02" name="street1" value="<?php echo $_SESSION['checkOut']['address-street1'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <!--<label class="control-label" for="input03">Address</label>-->
                <div class="controls">
                  <input type="text" class="span3" id="input03" name="street2" value="<?php echo $_SESSION['checkOut']['address-street2'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input04">City</label>
                <div class="controls">
                  <input type="text" class="span3" id="input04" name="city" value="<?php echo $_SESSION['checkOut']['address-city'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input05">State</label>
                <div class="controls">
                  <input type="text" class="span3" id="input05" name="state" value="<?php echo $_SESSION['checkOut']['address-state'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input06">Country</label>
                <div class="controls">
                  <!--
                  <input type="text" class="span3" id="input06" name="country" value="<?php echo $_SESSION['checkOut']['address-country'];?>"<?php echo $form2_input_disable;?> />
                  -->
                  <select class="span3" id="input06" name="country"<?php echo $form2_input_disable;?>>
<?php 
  foreach ($tmpPostg as $postg) {
    if ($postg['postage_country_name'] == $_SESSION['checkOut']['address-country']) {
      $form_postage_country_select = " selected='selected'";
    } else {
      $form_postage_country_select = "";
    }
?>
                    <option value="<?php echo $postg['postage_country_name'];?>"<?php echo $form_postage_country_select; ?>><?php echo $postg['postage_country_name'];?></option>
<?php 
  } 
?>
                  </select>      
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input07">Postcode</label>
                <div class="controls">
                  <input type="text" class="span3" id="input07" name="postcode" value="<?php echo $_SESSION['checkOut']['address-postcode'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="input08">Phone</label>
                <div class="controls">
                  <input type="text" class="span3" id="input08" name="phone" value="<?php echo $_SESSION['checkOut']['address-phone'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              <!--
              <div class="control-group">
                <label class="control-label" for="input09">E-mail</label>
                <div class="controls">
                  <input type="text" class="span3" id="input09" name="email" value="<?php echo $_SESSION['checkOut']['address-email'];?>"<?php echo $form2_input_disable;?> />
                </div>
              </div>
              -->
              <div class="form-actions">
                <input type="submit" name="address-op" value="<?php echo $form2_btn_txt;?>" class="btn <?php echo $form2_btn_clr;?>" />
                <a<?php echo $form_continue_url; ?> class="btn btn-success<?php echo $form_continue_disable; ?>">Continue &raquo;</a>
              </div>
            </fieldset>
          </form>
<?php
?>